{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter, defaultdict\n",
    "from kneed import KneeLocator\n",
    "from sklearn.cluster import KMeans\n",
    "from matplotlib.patches import Rectangle\n",
    "from vid_utils import *\n",
    "\n",
    "import numpy as np \n",
    "import cv2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import json\n",
    "from os import path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('result.json', 'r') as f:\n",
    "    D = json.load(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## Extract Objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "All_Cords = extract_objects(D)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extract Cases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "AT = extract_cases(All_Cords)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Detect Change in Camera"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "Base = \"processed_images/\"\n",
    "if path.exists(\"change.npy\"):\n",
    "    change_cam,loc,Cstat = np.load(\"change.npy\")\n",
    "else:\n",
    "    change_cam, loc,Cstat = change_detect(Base)\n",
    "    np.save(\"change.npy\",[change_cam,loc,Cstat])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "PT = list(set(AT) - set(change_cam))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Case 1: Extract ROI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "if path.exists(\"centers1.npy\"):\n",
    "    Centers = np.load(\"centers1.npy\",allow_pickle=True)\n",
    "else:\n",
    "    Centers = extract_roi(PT,All_Cords)\n",
    "    np.save(\"centers1.npy\",Centers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "56"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(Centers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Case 1: Extract Bounds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "if path.exists(\"bounds1.npy\"):\n",
    "    Bounds = np.load(\"bounds1.npy\",allow_pickle=True)\n",
    "else:\n",
    "    Bounds = extract_bounds(Centers,PT,All_Cords)\n",
    "    np.save(\"bounds1.npy\",Bounds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "95"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(Bounds)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Case 1: Backtracking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "Base = \"ori_images/\"\n",
    "\n",
    "if path.exists(\"result1.npy\"):\n",
    "    Times, Stat = np.load(\"result1.npy\",allow_pickle=True)\n",
    "else:\n",
    "    Times, Stat = backtrack(Bounds,PT,Base)\n",
    "    np.save(\"result1.npy\",[Times,Stat])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Case 2: Extract ROI "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "if path.exists(\"centers2.npy\"):\n",
    "    Centers2 = np.load(\"centers2.npy\",allow_pickle=True)\n",
    "else:\n",
    "    Centers2 = extract_roi1(change_cam,All_Cords,loc)\n",
    "    np.save(\"centers2.npy\",Centers2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Case 2: Extract Bounds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "if path.exists(\"bounds2.npy\"):\n",
    "    Bounds2 = np.load(\"bounds2.npy\",allow_pickle=True)\n",
    "else:\n",
    "    Bounds2 = extract_bounds1(Centers2,change_cam,loc,All_Cords)\n",
    "    np.save(\"bounds2.npy\",Bounds2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Case 2: Backtracking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(56, 12)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(Centers),len(Centers2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(95, 18)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(Bounds),len(Bounds2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n"
     ]
    }
   ],
   "source": [
    "Base = \"ori_images/\"\n",
    "\n",
    "\n",
    "if path.exists(\"result2.npy\"):\n",
    "    Times2, Stat2 = np.load(\"result2.npy\")\n",
    "else:\n",
    "    Times2, Stat2 = backtrack1(Bounds2,Base)\n",
    "    np.save(\"result2.npy\",[Times2,Stat2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "Times = {key:val for key, val in Times.items() if val != 999}\n",
    "Times = {key:val for key, val in Times.items() if val >= 40}\n",
    "\n",
    "Times2 = {key:val for key, val in Times2.items() if val != 999}\n",
    "Times2 = {key:val for key, val in Times2.items() if val >= 40}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "file1 = open(\"Result\" + \".txt\",\"w\")\n",
    "for x in Times:\n",
    "    file1.write('{0:2d} {1:3d} {2:1d}'.format(x,int(Times[x]),1))\n",
    "    file1.write(\"\\n\")\n",
    "    \n",
    "for x in Times2:\n",
    "    file1.write('{0:2d} {1:3d} {2:1d}'.format(x,int(Times2[x]),1))\n",
    "    file1.write(\"\\n\")\n",
    "    \n",
    "file1.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
